package com.bey.bow.web.video;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bey.bow.entity.User;
import com.bey.bow.entity.Video;
import com.bey.bow.service.video.VideoService;
import com.bey.bow.utils.FileNameGenerator;
import com.bey.bow.utils.FilePathUtil;
import com.bey.bow.utils.UserUtils;

@Controller
@RequestMapping(value = "admin/video")
public class VideoAdminController {

	private VideoService videoService;

	private static final int PAGE_SIZE = 10;

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("video", new Video());
		model.addAttribute("action", "create");
		return "admin/video/videoForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(Video video,
			@RequestParam("file") MultipartFile file,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {

		if (file.isEmpty()) {
			return "/admin/course/course_add_fail";
		}

		String fileName = FileNameGenerator.getNewFileName(file);
		String filePath = FilePathUtil.filePath + "/" + fileName;
		video.setVideoImg(fileName);
		File dest = new File(filePath);

		try {
			file.transferTo(dest);
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return "/admin/course/course_add_fail";
		} catch (IOException e) {
			e.printStackTrace();
			return "/admin/course/course_add_fail";
		}

		User user = new User(UserUtils.getCurrentUserId());
		video.setUser(user);

		videoService.saveVideo(video);
		redirectAttributes.addFlashAttribute("message", "创建任务成功");
		return "redirect:/admin/video/";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("video", videoService.getVideo(id));
		model.addAttribute("action", "update");
		return "admin/video/videoForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("video") Video video,
			RedirectAttributes redirectAttributes) {

		User user = new User(UserUtils.getCurrentUserId());
		video.setUser(user);

		videoService.saveVideo(video);
		redirectAttributes.addFlashAttribute("message", "更新任务成功");
		return "redirect:/admin/video/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		videoService.deleteVideo(id);
		redirectAttributes.addFlashAttribute("message", "删除课程成功");
		return "redirect:/admin/video/";
	}

	@RequestMapping(value = "")
	public String list(
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			Model model, ServletRequest request) {

		Page<Video> videos = videoService.getVideos(pageNumber,
				PAGE_SIZE, "auto");

		model.addAttribute("videos", videos);

		return "admin/video/videoList";
	}

	@Autowired
	public void setVideoService(VideoService videoService) {
		this.videoService = videoService;
	}

}
